
export default class Recycle {
    constructor (capacity) {
        this.capacity = capacity
        this.data = []
    }
    *[Symbol.iterator] () {
        for (let i = this.data.length - 1; i >= 0; i--) {
            yield this.data[i]
        }
    }
    add (bage) {
        if (this.data.length === this.capacity) {
            let data = this.data.shift()
            if ([...data.values()][0].subject.on_destroy) {
                [...data.values()][0].subject.on_destroy.call([...data.values()][0].subject)
            }
        }
        this.data.push(bage)
    }
    recycle (bage) {
        let pos = this.data.indexOf(bage)
        this.data.splice(pos, 1)
        return bage
    }
}